package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.eastjet.equipment.service.DeviceFrrService;
import com.eastjet.maintenance.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/frr")
public class FrrController {

    @Autowired
    private DeviceFrrService deviceFrrService;

    @RequestMapping("/list")
    public Page<DeviceFrr>  getDeviceFrrList(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "2") int size,
                                             @RequestParam(defaultValue ="") Map<String,Object> map)
    {
        System.out.println("map:" + map);
//        Page<DeviceFrr> pageData = deviceFrrService.getDeviceFrrList(page, size, condition);
        return deviceFrrService.getDeviceFrrList(page, size, map);
    }


    @RequestMapping("/add")
    public String addFT( @RequestBody Map<String, Object> map) {

        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else
        {
            TableName = (String) map.get("TableName");
            DeviceFrr deviceFrr = new DeviceFrr();
            deviceFrr.setFrrType(Integer.valueOf( map.get("type").toString()));
            deviceFrr.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceFrr.setFrrTopic((String) map.get("topic"));
            deviceFrr.setFrrPerson((String) map.get("person"));
            deviceFrr.setFrrTime((Date) map.get("time"));
            deviceFrr.setFrrBadtime((Date) map.get("badtime"));
            deviceFrr.setFrrBadgrade((String) map.get("badgrade"));
            deviceFrr.setFrrBadtype(Integer.valueOf((String) map.get("badtype")));
            deviceFrr.setIsStop(Integer.valueOf( map.get("isStop").toString()));
            deviceFrr.setFrrEquipmentId(Integer.valueOf( map.get("equipmentId").toString()));
            deviceFrr.setRelevancy((String) map.get("relevancy"));
            deviceFrr.setNotes((String) map.get("notes"));
            System.out.println("deviceFrr:" + deviceFrr);
            return   deviceFrrService.addDeviceFrr(deviceFrr,TableName);

        }

    }


    @RequestMapping("/delete")
    public String deleteFT( @RequestBody  Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceFrr deviceFrr = new DeviceFrr();
        deviceFrr.setFrrId(Integer.valueOf(map.get("id").toString()));
        deviceFrr.setIsDelete(Integer.valueOf(map.get("isDelete").toString()));
        return  deviceFrrService.deleteDevicFrr(deviceFrr);
    }

    @RequestMapping("/getPerson")
    public List<DeviceUser> getPerson()
    {
      return   deviceFrrService.getDeviceUserList();
    }


    @RequestMapping("/getDeptAddPhone")
    public List<DeviceUser> getDeptAddPhone(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return null;
        }
        DeviceUser deviceUser = new DeviceUser();
        deviceUser.setUserName((String) map.get("name"));
      return   deviceFrrService.getDeptAddPhone(deviceUser);
    }


    @RequestMapping("/getEquipmentList")
    public List<Equipment> getDeviceFrrList(@RequestParam Map<String,Object> map)
    {
        System.out.println("map:" + map);

        String condition = (String) map.get("condition");
        return deviceFrrService.getEquipmentList(condition);
    }
}
